﻿using System;
using System.Configuration;
using System.Reflection;

namespace Shiyan10
{

    //用户类
    class User
    {
        private int _id;
        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }

    //Iuser接口，用于客户端访问，解除与具体数据库访问的耦合
    interface IUser
    {
        void Insert(User user);
        User GetUser(int id);
        
    }

    //部门表
    class Department
    {
        private int _id;
        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }
        private string _deptName;
        public string DeptName
        {
            get { return _deptName; }
            set { _deptName = value; }
        }
    }

    //用于客户端访问，解除与数据库的耦合
    interface IDepartment
    {
        void Insert(Department department);
        Department GetDeptment(int id);
    }

    //SqlServerDepartment类，用于访问SQL Server的Department
    class SqlserverDepartment : IDepartment
    {
        public Department GetDeptment(int id)
        {
            Console.WriteLine("在SQL Server根据ID从Department表得到一条记录。");
            return null;
        }

        public void Insert(Department department)
        {
            Console.WriteLine("在SQL Server中给User表增加一条记录。");
        }
    }

    //AccessDepartment类，用于访问Access的Department
    class AccessDepartment : IDepartment
    {
        public Department GetDeptment(int id)
        {
            Console.WriteLine("在Access根据ID从Department表得到一条记录。");
            return null;
        }

        public void Insert(Department department)
        {
            Console.WriteLine("在Access中给Department增加一条记录。");
        }
    }


    //SqlServerUser类，用于访问SQL Server的User
    class SqlserverUser : IUser
    {
        public User GetUser(int id)
        {
            Console.WriteLine("在SQL Server根据ID从User表得到一条记录。");
            return null;
        }

        public void Insert(User user)
        {
            Console.WriteLine("在SQL Server中给User表增加一条记录。");
        }
    }

    //Access类，用于访问Access的User
    class AccessUser : IUser
    {
        public User GetUser(int id)
        {
            Console.WriteLine("在Access根据ID从User表得到一条记录。");
            return null;
        }

        public void Insert(User user)
        {
            Console.WriteLine("在Access中给User表增加一条记录。");
        }
    }

    //DataAccess类，用来代替抽象工厂的IFactory、SqlserverFactory、AccessFavtory
    class DataAccess
    {
        //程序集的名称
        private static readonly string AssmblyName = "Shiyan10";
        //数据库的名称
        private static readonly string db = ConfigurationManager.AppSettings["DB"];

        public static IUser CreaterUSer()
        {
            string className = AssmblyName + "." + db + "User";

            return (IUser)Assembly.Load(AssmblyName).CreateInstance(className);
        }

        public static IDepartment CreateDepartment()
        {
            string className = AssmblyName + "." + db + "Department";
            return (IDepartment)Assembly.Load(AssmblyName).CreateInstance(className);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            User user = new User();
            Department department = new Department();

            IUser iu = DataAccess.CreaterUSer();
            iu.Insert(user);
            iu.GetUser(1);

            IDepartment id = DataAccess.CreateDepartment();
            id.Insert(department);
            id.GetDeptment(1);

            Console.Read();
        }
    }
}
